package com.dreamlike.demo.HttpUtil.Core.Validator.impl;

import com.dreamlike.demo.HttpUtil.Core.HttpDefinition;
import com.dreamlike.demo.HttpUtil.Core.Validator.HttpDefinitionValidator;
import com.dreamlike.demo.HttpUtil.Core.Validator.HttpDefinitionValidateResult;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.concurrent.Future;

public class ReturnSingleGenericValidator implements HttpDefinitionValidator {

    @Override
    public boolean validate(HttpDefinition httpDefinition, HttpDefinitionValidateResult h) {
        Method method = httpDefinition.getMethod();
        Type genericReturnType = method.getGenericReturnType();
        Type actualType = genericReturnType;
        if (Future.class.isAssignableFrom(method.getReturnType())){
            //纯Future
            if (genericReturnType instanceof Class){
                h.addInfo(httpDefinition.getOriginalMethodDesc()+"Future like 返回值必须带单层泛型",this);
                return false;
            }
            actualType = ((ParameterizedType) genericReturnType).getActualTypeArguments()[0];
        }
        if (actualType instanceof ParameterizedType){
            for (Type type : ((ParameterizedType) actualType).getActualTypeArguments()) {
                if (type instanceof ParameterizedType) {
                    h.addInfo(httpDefinition.getOriginalMethodDesc()+"非future子类返回值必须为单层泛型",this);
                    return false;
                }
            }

        }
        return true;
    }

}
